// CtlTagList.cpp : implementation file
//

#include "stdafx.h"
#include "bdc.h"
#include "CtlTagList.h"

using namespace bdc::ctl;

// CtlTagList

IMPLEMENT_DYNAMIC(CtlTagList, CListCtrl)

CtlTagList::CtlTagList()
{

}

CtlTagList::~CtlTagList()
{
}


BEGIN_MESSAGE_MAP(CtlTagList, CListCtrl)
	ON_WM_CREATE()
	ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, &CtlTagList::OnNMCustomdraw)
	ON_WM_MEASUREITEM_REFLECT()
END_MESSAGE_MAP()



// CtlTagList message handlers




BOOL CtlTagList::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Add your specialized code here and/or call the base class

	cs.style &= ~LVS_TYPEMASK;
	cs.style |= LVS_REPORT | LVS_NOCOLUMNHEADER | LVS_OWNERDRAWFIXED;

	return CListCtrl::PreCreateWindow(cs);
}


int CtlTagList::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CListCtrl::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  Add your specialized creation code here
	HINSTANCE hInst = ((CbdcApp*) AfxGetApp())->hPicResource;
	pImgBg = bdc::LoadPng(hInst, IDB_LIB_TAG_BG);

	return 0;
}


void CtlTagList::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMLVCUSTOMDRAW pNMCD = reinterpret_cast<LPNMLVCUSTOMDRAW>(pNMHDR);
	// TODO: Add your control notification handler code here
	NMCUSTOMDRAW customDraw = pNMCD->nmcd;

	if(customDraw.dwDrawStage == CDDS_PREPAINT) {
		*pResult = CDRF_NOTIFYITEMDRAW;
	}
	else if(customDraw.dwDrawStage == CDDS_ITEMPREPAINT) {
		*pResult = CDRF_SKIPDEFAULT;

		Gdiplus::Graphics graphics(customDraw.hdc);

		BOOL isChecked = (customDraw.uItemState & CDIS_SELECTED);

		Gdiplus::Rect rect(customDraw.rc.left, customDraw.rc.top, LIB_TL_ITEM_WIDTH, LIB_TL_ITEM_HEIGHT);
		graphics.DrawImage(pImgBg, rect, 0, isChecked ? LIB_TL_ITEM_HEIGHT : 0, LIB_TL_ITEM_WIDTH, LIB_TL_ITEM_HEIGHT, Gdiplus::UnitPixel);

		Gdiplus::SolidBrush  brush(Gdiplus::Color(255, 0, 0, 0));
		Gdiplus::FontFamily  fontFamily(L"Times New Roman");
		Gdiplus::Font        font(&fontFamily, 14, Gdiplus::FontStyleRegular, Gdiplus::UnitPixel);
		Gdiplus::PointF      pointF(10.0f, customDraw.rc.top + 8.0f);

		CString string = this->GetItemText(customDraw.dwItemSpec, 0);
		graphics.DrawString(string.GetString(), -1, &font, pointF, &brush);
	}
	else {
		*pResult = CDRF_DODEFAULT;
	}
}


void CtlTagList::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
	// TODO: Add your message handler code here and/or call default
	lpMeasureItemStruct->itemHeight = 35;
}
